home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12259 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Variant Records in C ... Is there a way ?
  5. Date: Sat, 30 Mar 96 00:18:16 GMT
  6. Organization: none
  7. Message-ID: <828145096snz@genesis.demon.co.uk>
  8. References: <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt> <315006F8.639@cmt.lpr.mail.carel.fi> <4iumf8$604@madeline.INS.CWRU.Edu> <danpop.827534657@rscernix> <4jcrbm$g5p@madeline.INS.CWRU.Edu>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jcrbm$g5p@madeline.INS.CWRU.Edu>
  15.            mab22@po.CWRU.Edu "Michael A. Balfour" writes:
  16.  
  17. >
  18. >In a previous article, danpop@mail.cern.ch (Dan Pop) says:
  19. >
  20. >>In <4iumf8$604@madeline.INS.CWRU.Edu> mab22@po.CWRU.Edu (Michael A. Balfour)
  21. > writes:
  22. >>
  23. >>>*However*, if you feel like it, you can still do it yourself with some
  24. >>>clever mallocs and overlaying structures.  Not exactly elegant, but you
  25. >>>can make it pretty memory-efficient.
  26. >>
  27. >>Not exactly blessed by the C standard, either :-)
  28. >>
  29. >
  30. >Maybe not blessed, but it's certainly not condemned.
  31.  
  32. You'll almost certainly invoke undefined behaviour as far as the language
  33. is concerned. So it is in the same category as void main() or i = i++,
  34. i.e. condemned to be non-portable.
  35.  
  36. > That's the beauty
  37. >of pointers and typecasting (which my COBOL friends envy).  But I
  38. >suppose you've never had to do anything like this:
  39. >
  40. >int temp=1;
  41. >char *temp2=(char *)&temp;
  42. >#define ByteOrder() ((temp2[0]==0x01) ? LITTLE_ENDIAN : BIG_ENDIAN)
  43.  
  44. Probably better as:
  45.  
  46. const int temp = 1;
  47.  
  48. #define ByteOrder() ((*(unsigned char *)&temp==1) ? LITTLE_ENDIAN : BIG_ENDIAN)
  49.  
  50. The compiler might even optimise that to a compile time constant.
  51.  
  52. >Or this:
  53. >
  54. >char buffer[BUF_SIZE];
  55. >int bufEndPtr;
  56. >
  57. >...
  58. >
  59. >void AddIntToBuffer(int num)
  60. >{
  61. >  short i;
  62.  
  63. It is usually better to use int/unsigned for auxiliary variables - that
  64. is the type that corresponds to the 'natural size suggested by the
  65. archetecture of the execution environment' and is the best bet for
  66. efficiency.
  67.  
  68. >  for (i=0;i<sizeof(int);i++)  buffer[bufEndPtr++]=((char *)(&num))[i];
  69. >}
  70.  
  71. You're safer working with unsigned char arrays.
  72.  
  73. >void AddStringToBuffer(char *s,int len)
  74. >{
  75. >  short i;
  76. >  for (i=0;i<len;i++)  buffer[bufEndPtr++]=s[i];
  77. >}
  78.  
  79. The language does give license to access any object through character
  80. lvalues. It has been suggested that future revisions of the standard will
  81. clarify the issue so that, say copying an object as an array of unsigned char
  82. works (as long as you end up with suitable alignment). Anyway this is a better
  83. approach than using unions.
  84.  
  85. >But there are plenty of cases where you might need to go walking through
  86. >a patch of memory that contains data all packed together.  So like I
  87. >said originally, it might not be the most elegant solution, but it can
  88. >certainly be made functional.  And you can write it in ANSI C.
  89.  
  90. That doesn't mean that ANSI C will guarantee that your code does what
  91. you expect.
  92.  
  93. -- 
  94. -----------------------------------------
  95. Lawrence Kirby | fred@genesis.demon.co.uk
  96. Wilts, England | 70734.126@compuserve.com
  97. -----------------------------------------
  98.